package cn.itcast.springboot.controller;

import cn.itcast.springboot.pojo.User;
import cn.itcast.springboot.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;
    @GetMapping("findAll") // 接收前端  get
    @PreAuthorize("hasRole('QUERY')")
    public List<User> findAll(){

        List<User> userList = userService.list();
        return   userList;
    }
    @PostMapping("save")  // 接收前端 post请求
    @PreAuthorize("hasAuthority('save')")
    public String save(@RequestBody User user){
        boolean flag = userService.save(user);
        return  flag+"";
    }

    @PutMapping("update")  //  接收前端  put 请求
    @PreAuthorize("hasAuthority('update')")
    public String update(@RequestBody User user){
        boolean flag =  userService.updateById(user);
        return  flag+"";
    }

    @DeleteMapping("delete/{id}") // 接收前端 删除请求
    @PreAuthorize("hasAuthority('delete')")
    public String  delete(@PathVariable("id") int  id){
        boolean flag =  userService.removeById(id);
        return  flag+"";
    }

}
